//
//  RecommendCycleView.m
//  YuDou
//
//  Created by qiupeng on 2018/6/23.
//  Copyright © 2018年 yingqi. All rights reserved.
//

#import "RecommendCycleView.h"
#import "CollectionCycleCell.h"

#import "CycleModel.h"


@interface RecommendCycleView ()<UICollectionViewDataSource,UICollectionViewDelegate>

@property (nonatomic ,weak) IBOutlet UICollectionView *collectionView;
@property (nonatomic ,weak) IBOutlet UIPageControl *pageControl;
@property (nonatomic ,strong) NSTimer *cycleTimer;
@end

@implementation RecommendCycleView

+ (instancetype)recommendCycleView
{
    return [[[NSBundle mainBundle] loadNibNamed:@"RecommendCycleView" owner:nil options:nil] firstObject];
}

- (void)awakeFromNib
{
    [super awakeFromNib];
    
    self.autoresizingMask = UIViewAutoresizingNone;
    self.collectionView.dataSource = self;
    self.collectionView.delegate = self;
    [self.collectionView registerNib:[UINib nibWithNibName:@"CollectionCycleCell" bundle:nil] forCellWithReuseIdentifier:CycleCellID];

}



- (void)setCycleModels:(NSArray *)cycleModels
{
    _cycleModels = cycleModels;

    [self.collectionView reloadData];
    NSInteger count  = _cycleModels.count ? _cycleModels.count : 0;
    self.pageControl.numberOfPages = count;
    NSIndexPath *indexPath = [NSIndexPath indexPathForItem:count * 100 inSection:0];
    [self.collectionView scrollToItemAtIndexPath:indexPath atScrollPosition:UICollectionViewScrollPositionLeft animated:NO];
    
    [self removeCycleTimer];
    [self addCycleTimer];
    
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    layout.itemSize =  self.collectionView.bounds.size;
    layout.minimumLineSpacing = 0;
    layout.minimumInteritemSpacing = 0;
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    self.collectionView.collectionViewLayout = layout;
    self.collectionView.pagingEnabled = YES;
    self.collectionView.showsHorizontalScrollIndicator = NO;
}

#pragma mark - CollecetionView DataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    NSInteger count  = _cycleModels.count ? _cycleModels.count : 0;
    return count * 1000;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    CollectionCycleCell *cell = (CollectionCycleCell *)[collectionView dequeueReusableCellWithReuseIdentifier:CycleCellID forIndexPath:indexPath];
    cell.cycleModel = _cycleModels[(indexPath.item % _cycleModels.count)];
    return cell;
}

#pragma mark - CollecetionView Delegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    NSInteger count  = _cycleModels.count ? _cycleModels.count : 1;
    NSInteger index = (scrollView.contentOffset.x + scrollView.width * 0.5) / scrollView.width;
    self.pageControl.currentPage = index % count;
}

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    [self removeCycleTimer];
}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
    [self addCycleTimer];
}


- (void)addCycleTimer
{
    _cycleTimer = [NSTimer timerWithTimeInterval:3.0 target:self selector:@selector(scrollToNext) userInfo:nil repeats:YES];
    [[NSRunLoop currentRunLoop] addTimer:_cycleTimer forMode:NSRunLoopCommonModes];
}

- (void)removeCycleTimer
{
    [_cycleTimer invalidate];
    _cycleTimer = nil;
}

- (void)scrollToNext
{
    [self.collectionView setContentOffset:CGPointMake(self.collectionView.contentOffset.x + self.collectionView.width, 0) animated:YES];
}
@end
